package org.mspring.platform.denglu.api;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.mspring.platform.denglu.Constants;
import org.mspring.platform.denglu.DengluUtils;
import org.mspring.platform.denglu.entity.dto.DengluComment;
import org.mspring.platform.utils.http.HttpUtils;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;

/**
 * 灯鹭评论相关服务
 *
 * @author Gao Youbo
 * @since 2014-05-26 13:54:56
 */
@Component
public class DengluCommentApiService {

    /**
     * http://developer.denglu.cc/title=%E8%AF%84%E8%AE%BA%E5%88%97%E8%A1%A8
     *
     * @param commentId
     * @param count
     * @return
     * @throws java.io.IOException
     * @throws java.net.URISyntaxException
     */
    public List<DengluComment> getCommentList(long commentId, int count) throws IOException, URISyntaxException {
        Map<String, String> params = new TreeMap<>();
        params.put("appid", Constants.APPID);
        params.put("version", Constants.VERSION);
        params.put("sign_type", Constants.SIGN_TYPE);
        params.put("timestamp", String.valueOf(System.currentTimeMillis()));
        if (commentId > 0) {
            params.put("commentid", String.valueOf(commentId));
        }
        if (count > 0) {
            params.put("count", String.valueOf(count));
        }
        String sign = DengluUtils.sign(params);
        params.put("sign", sign);
        HttpResponse httpResponse = HttpUtils.get(Constants.GET_COMMENT_LIST, params);
        String result = EntityUtils.toString(httpResponse.getEntity());
        List<DengluComment> dengluComments = JSON.parseArray(result, DengluComment.class);
        return dengluComments;
    }

    public static void main(String[] args) throws Exception {
        new DengluCommentApiService().getCommentList(0, 0);
    }
}
